package com.zhehekeji.opsys.api.plugin.wechat.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


/**
 * @ClassName HttpUtils
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2017-12-22   下午1:48:16
 */
@SuppressWarnings("deprecation")
public class HttpUtils {
	
	public static String get(String url) throws ClientProtocolException, IOException {
		CloseableHttpClient httpClient = null;
		String body = null;
		try {
			httpClient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(url);
			CloseableHttpResponse response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			body = EntityUtils.toString(entity, StandardCharsets.UTF_8);
		} finally {
			if (httpClient != null){
				httpClient.close();
			}
		}
		return body;
	}
	
	public static String postForm(String url, String param) throws IOException {
		CloseableHttpClient httpClient = null;
		String body = null;
		try {
			httpClient = HttpClients.createDefault();
			HttpPost httPost = new HttpPost(url);
			StringEntity reqEntity = new StringEntity(param, StandardCharsets.UTF_8.name());
			reqEntity.setContentEncoding(StandardCharsets.UTF_8.name());
			reqEntity.setContentType(ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
			httPost.setEntity(reqEntity);
			CloseableHttpResponse response = httpClient.execute(httPost);
			HttpEntity rspEntity = response.getEntity();
			body = EntityUtils.toString(rspEntity, StandardCharsets.UTF_8);
		} finally {
			if (httpClient != null){
				httpClient.close();
			}
		}
		return body;
	}
	
	public static String postJson(String url, String param) throws IOException {
		CloseableHttpClient httpClient = null;
		String body = null;
		try {
			httpClient = HttpClients.createDefault();
			HttpPost httPost = new HttpPost(url);
			StringEntity reqEntity = new StringEntity(param, StandardCharsets.UTF_8.name());
			reqEntity.setContentEncoding(StandardCharsets.UTF_8.name());
			reqEntity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
			httPost.setEntity(reqEntity);
			CloseableHttpResponse response = httpClient.execute(httPost);
			HttpEntity rspEntity = response.getEntity();
			body = EntityUtils.toString(rspEntity, StandardCharsets.UTF_8);
		} finally {
			if (httpClient != null){
				httpClient.close();
			}
		}
		return body;
	}
	
	public static String postFormSSL(String url, String param, String certLocation, String certPassword) throws ParseException, IOException, 
		KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException{
		FileInputStream instream = null;
		CloseableHttpClient httpClient = null;
		String body = null;
		try {
			KeyStore keyStore  = KeyStore.getInstance("PKCS12");
			instream = new FileInputStream(new File(certLocation));
	        keyStore.load(instream, certPassword.toCharArray());
	        // Trust own CA and all self-signed certs
	        SSLContext sslcontext = SSLContexts.custom()
	                .loadKeyMaterial(keyStore, certPassword.toCharArray())
	                .build();
	        // Allow TLSv1 protocol only
	        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
	                sslcontext,
	                new String[] { "TLSv1" },
	                null,
	                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
	        httpClient = HttpClients.custom()
	                .setSSLSocketFactory(sslsf)
	                .build();
			HttpPost httPost = new HttpPost(url);
		
			StringEntity reqEntity = new StringEntity(param, StandardCharsets.UTF_8.name());
			reqEntity.setContentEncoding(StandardCharsets.UTF_8.name());
			reqEntity.setContentType(ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
			httPost.setEntity(reqEntity);
			CloseableHttpResponse response = httpClient.execute(httPost);
			HttpEntity rspEntity = response.getEntity();
			body = EntityUtils.toString(rspEntity, StandardCharsets.UTF_8);
		} finally {
			if (instream != null){
				instream.close();
			}
			if (httpClient != null){
				httpClient.close();
			}
			
		}
		return body;
	}
	
}

